package com.project.class3_30;

import java.util.concurrent.*;

public class ThreadTest {
    public static void main(String[] args) {
        FutureTask<Integer> task= new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum= 0;
                for (int i=0;i<20;i++){
                    sum+=i;
                }
                return sum;
            }
        });
        //启动线程
        new Thread(task).start();
        try {
            Integer x= task.get();
            System.out.println(x);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //创建无限扩展线程池
        ExecutorService es= Executors.newCachedThreadPool();

        //创建固定大小的线程池。如果任务数量超过空闲线程池，多余任务等待
//        ExecutorService es= Executors.newFixedThreadPool(2);

        //创建单线程的线程池,如果有多个任务，顺序执行
//        ExecutorService es =Executors.newSingleThreadExecutor();
        //添加执行任务
//        es.execute(new one());
//        es.execute(new two());
//        es.execute(new three());
        //关闭线程池
        es.shutdown();
    }
}
class one implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<10;i++){
            System.out.print("*");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class two implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<10;i++){
            System.out.print("#");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class three implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<10;i++){
            System.out.println("$");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}